<?php


namespace app\api\controller\v1;


use app\api\model\BikeMgr;
use app\api\model\BikeWhRecordModel;
use app\common\library\DLock;
use app\common\library\UMaleModel;
use common;
use app\api\model\CityModel;
use app\api\model\BikeModel;
use app\common\controller\UserBaseController;

class BinvController extends UserBaseController {

    function addAction() {
        $this->paramlist = array(
            'token' => 'string',
            'bikes' => 'array',
        );

        $this->check_params();
        $this->check_token();
        $city_info = (new CityModel())->query_info($this->city_id);
        if (empty($city_info)) {
            $this->throw_myexception(X_ERROR, "查询城市失败!");
        }

        $bike_param_list = [
            'city_id' => 'string',
            'warehouse_id' => 'string',
            'warehouse_name' => 'string',
            'sn' => 'string',
            'brand' => 'string',
            'model' => 'string',
            'imei' => 'string',
            'ecu_sn' => 'string',
            'ecu_brand' => 'string',
            'ecu_model' => 'string',
            'product_batch_number' => 'string',
            'brand' => 'string',
            'battery_capacity' => 'int',
        ];

        $i = 0;
        foreach ($this->argv['bikes'] as $item) {
            $i++;
            if ($item['city_id'] != $this->city_id) {
                $this->throw_myexception(X_ERROR, " 第{$i}条城市ID错误!");
            }
            $what = null;
            $bikeinfo = common::check_json_keys($item, $bike_param_list, $what);
            if (empty($bikeinfo)) {
                $this->throw_myexception(X_ERROR, "第{$i}条 校验车辆字段失败, {$what}!");
            }
            $bike = (new BikeModel())->get_info_by_imei($bikeinfo['imei']);
            if (!empty($bike)) {
                $this->throw_myexception(X_ERROR, "第{$i}条 IMEI 重复!");
            }
            $bike = (new BikeModel())->get_info_by_sn($bikeinfo['sn']);
            if (!empty($bike)) {
                $this->throw_myexception(X_ERROR, "第{$i}条 bike sn 重复!");
            }
        }


        foreach ($this->argv['bikes'] as $item) {
            $bikeinfo = common::check_json_keys($item, $bike_param_list, $what);
            (new BikeMgr())->add_bike($bikeinfo);
        }
    }

    function importAction() {
        $this->paramlist = array(
            'token' => 'string',
            'bike_sn' => 'string',
            'ecu_sn' => 'string',
            'imei' => 'string',
            'model' => ['optstring', 'Q5'],
        );

        $this->check_params();
        $this->check_token();

        $imei = $this->argv['imei'];

        $bike_sn = $this->argv['bike_sn'];
        $ecu_sn = $this->argv['ecu_sn'];
        $model = $this->argv['model'];


        if (strlen($bike_sn) != 8) {
            $this->throw_myexception(X_ERROR, "SN格式错误!");
        }


        $ecu_model = null;
        $ecu_brand = null;
        $ecu_info = null;
        if (empty($imei)) {//泰比特条形码没有IMEI信息
            $ecu_info = (new BikeModel())->get_ecu_info_by_sn($ecu_sn);
            if (empty($ecu_info)) {
                $ecu_info = [
                    'imei' => $ecu_sn,
                    'sn' => $ecu_sn,
                    'model' => ECU_MODEL_TBIT_209D,
                    'brand' => 'TBIT',
                ];
            }
        } else {
            if (strlen($imei) != strlen('866039042496290')) {
                $this->throw_myexception(X_ERROR, "IMEI格式错误!");
            }
            $ecu_info = (new BikeModel())->get_ecu_info_by_imei($imei);
            if (empty($ecu_info)) {
                $ecu_info = [
                    'imei' => $imei,
                    'sn' => $ecu_sn,
                    'model' => ECU_MODEL_XIAOAN_ATMX3F,
                    'brand' => 'XIAOAN',
                ];
            }
        }

        if (empty($ecu_info)) {
            $this->throw_myexception(X_ERROR, "查询ecu信息失败!");
        }


        $l1 = new DLock($bike_sn, 30);
        $l2 = new DLock($imei, 30);

        $bike = (new BikeModel())->get_info_by_imei($imei);
        if (!empty($bike)) {
            $this->throw_myexception(X_ERROR, "IMEI已存在!");
        }

        $bike = (new BikeModel())->get_info_by_sn($bike_sn);
        if (!empty($bike)) {
            $this->throw_myexception(X_ERROR, "SN已存在!");
        }

        $bike = (new BikeModel())->get_info_by_ecu_sn($ecu_sn);

        if (!empty($bike)) {
            $this->throw_myexception(X_ERROR, "SN已存在!");
        }
        $brand = null;
        $battery_model = null;
        if ($model == 'Q5') {
            $brand = '富士达';
            $battery_model = 'TN48V20AH';
        }
        else if ($model =='X3'){
            $brand = '华彪';
            $battery_model = 'TN48V15AH';
        }
        else {
            $brand = '雅迪';
            $battery_model = 'XH48014J';
        }

        $bikeinfo = [
            'city_id' => $this->city_id,
            'sn' => $bike_sn,
            'brand' => $brand,
            'model' => $model,
            'imei' => $ecu_info['imei'],
            'ecu_sn' => $ecu_info['sn'],
            'ecu_brand' => $ecu_info['brand'],
            'ecu_model' => $ecu_info['model'],
            'product_batch_number' => date('ymd'),
            'battery_capacity' => 12800,
            'warehouse_id' => null,
            'warehouse_name' => null,
            'battery_model' => $battery_model,
            'bluetooth_token' => isset($ecu_info['bluetooth_token']) ? $ecu_info['bluetooth_token'] : NULL,
        ];

        (new BikeMgr())->add_bike($bikeinfo);
        (new BikeMgr())->query_bike_info_rt($bikeinfo['imei']);
    }

    function launchAction() {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $bike_info = (new BikeModel())->get_info_by_id($this->argv['bike_id']);

        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "query info error!");
        }

        if ($bike_info['inv_state'] != BIKE_INV_STATE_INFO_INIT && $bike_info['inv_state'] != BIKE_INV_STATE_DELIVING) {
            $this->throw_myexception(ERROR_STATE_DISMATCH, "车辆已投放!");
        }

        (new BikeMgr())->launch_to_market($this->city_id, $this->argv['bike_id']);

        if ($this->city_id == '451100'){
            (new UMaleModel())->bike_Info($bike_info,1,1);
        }
    }

    function deliverAction() {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $bike_info = (new BikeModel())->get_info_by_id($this->argv['bike_id']);

        if (empty($bike_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆信息失败!");
        }

        if ($bike_info['inv_state'] != BIKE_INV_STATE_INFO_INIT) {
            $this->throw_myexception(ERROR_STATE_DISMATCH, "车辆已投放或发货!");
        }

        (new BikeMgr())->deliver($this->city_id, $this->argv['bike_id']);
    }

    function listAction() {
        $this->paramlist = array(
            'token' => 'string',
            'bike_id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $list = (new BikeWhRecordModel())->query_list($this->city_id, $this->argv['bike_id'], $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;

        $this->resp['total'] = hisql::query_count();
    }

    function delAction() {
        $this->paramlist = [
            'id' => 'string',
        ];

        $this->check_params();
        $this->check_token();

        $bike_info = BikeModel::instance()->get_info_by_id($this->argv['id']);
        if (empty($bike_info) || $bike_info['is_deleted'] == 1) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询车辆信息失败！");
        }
        BikeModel::instance()->delete($this->argv['id']);

        if ($this->city_id == '451100'){
            (new UMaleModel())->bike_Info($bike_info,2,2);
        }
    }

}
